package service

import (
	"go-admin-server/app/dao"
	"go-admin-server/app/dao/model"
	"go-admin-server/common/assert"
)

var OperateLogService = new(operateLogService)

type operateLogService struct{}

func (s *operateLogService) Create(OperateType, Content, Describe, IpAddress string) (int64, error) {
	var log = new(model.OperateLog)
	log.Type = OperateType
	log.Describe = Describe
	log.Content = Content
	log.IpAddress = IpAddress
	log.UserId = 1
	err := dao.DB.Debug().Create(&log).Error
	return log.ID, err
}

func (s *operateLogService) List(page, size int) (list []*model.OperateLog, count int64) {
	db := dao.DB.Debug().Model(&model.OperateLog{})
	err := db.Count(&count).Error
	assert.AssertErrNil(err)
	err = db.Limit(size).Offset(size * (page - 1)).Order("created_at desc").Find(&list).Error
	assert.AssertErrNil(err)
	return list, count
}

